package com.langChain4j.modelManagement.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.base.frame.common.service.BaseServiceImpl;
import com.base.frame.exception.BusinessException;
import com.langChain4j.api.enums.CommonErrorEnum;
import com.langChain4j.api.enums.CommonStatusEnum;
import com.langChain4j.modelManagement.dto.ModelAccessConfigDTO;
import com.langChain4j.api.entity.ModelAccessConfig;
import com.langChain4j.modelManagement.mapper.ModelAccessConfigMapper;
import com.langChain4j.modelManagement.query.ModelAccessConfigQuery;
import com.langChain4j.modelManagement.service.ModelAccessConfigService;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 模型接入配置服务实现类
 *
 * @author linjun
 */
@Service
public class ModelAccessConfigServiceImpl extends BaseServiceImpl<ModelAccessConfig, ModelAccessConfigMapper> implements ModelAccessConfigService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ModelAccessConfig addModelAccessConfig(ModelAccessConfigDTO dto) {
        // 校验模型名称是否已存在且处于激活状态
        QueryWrapper<ModelAccessConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("model_name", dto.getModelName())
                .eq("is_active", CommonStatusEnum.STATUS_ENABLE.getCode());
        if (this.count(wrapper) > 0) {
            throw new BusinessException(CommonErrorEnum.MODEL_CONFIG_EXISTS);
        }
        // 构建实体并保存
        ModelAccessConfig entity = ModelAccessConfig.builder()
                .modelName(dto.getModelName())
                .modelType(dto.getModelType())
                .configJson(dto.getConfigJson())
                .isActive(CommonStatusEnum.STATUS_ENABLE.getCode())
                .createBy(dto.getCreateBy())
                .build();
        this.save(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ModelAccessConfig updateModelAccessConfig(ModelAccessConfigDTO dto) {
        // 根据ID查询模型配置是否存在
        ModelAccessConfig existing = this.getById(dto.getId());
        if (ObjectUtil.isNull(existing)) {
            throw new BusinessException(CommonErrorEnum.MODEL_CONFIG_NOT_EXISTS);
        }
        // 更新模型配置信息
        existing.setModelName(dto.getModelName());
        existing.setModelType(dto.getModelType());
        existing.setConfigJson(dto.getConfigJson());
        existing.setIsActive(dto.getIsActive());
        existing.setUpdateBy(dto.getUpdateBy());
        this.updateById(existing);
        return existing;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteModelAccessConfig(Long id) {
        // 根据ID查询模型配置是否存在
        ModelAccessConfig existing = this.getById(id);
        if (ObjectUtil.isNull(existing)) {
            throw new BusinessException(CommonErrorEnum.MODEL_CONFIG_NOT_EXISTS);
        }
        // 物理删除（实际项目中可根据需求改为标记删除）
        this.removeById(id);

    }

    @Override
    public List<ModelAccessConfig> getModelAccessConfigList(ModelAccessConfigQuery query) {
        QueryWrapper<ModelAccessConfig> wrapper = new QueryWrapper<>();
        if (query.getModelName() != null && !query.getModelName().isEmpty()) {
            wrapper.like("model_name", query.getModelName());
        }
        if (query.getModelType() != null && !query.getModelType().isEmpty()) {
            wrapper.like("model_type", query.getModelType());
        }
        if (query.getIsActive() != null) {
            wrapper.eq("is_active", query.getIsActive());
        }
        return this.list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ModelAccessConfig enableDisableModelAccessConfig(Long id, Integer isActive) {
        // 根据ID查询模型配置是否存在
        ModelAccessConfig existing = this.getById(id);
        if (ObjectUtil.isNull(existing)) {
            throw new BusinessException(CommonErrorEnum.MODEL_CONFIG_NOT_EXISTS);
        }
        // 更新模型的启用状态
        existing.setIsActive(isActive);
        this.updateById(existing);
        return existing;
    }
}